package view;

import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.util.*;

import model.*;
import control.*;

public class NewMember extends JPanel {
  public static final String NAME = "member";
  private SnakesCRUD parent;
  
  private ArrayList<Region> regions;
  private ArrayList<Member> members;
  
  private CMember memberControl;
  
  public NewMember(SnakesCRUD parent) {
    this.parent = parent;
    
    regions = new ArrayList<Region>();
    members = new ArrayList<Member>();
    memberControl = new CMember(parent.getConnection());
    
    initComponents();
    updateRegions();
    updateTable();
  }
  
  private final void updateTable() {
    try {
      this.members = memberControl.retrieveAll();
      
      Object[] o = new Object[5];
      ((DefaultTableModel)(tbMembers.getModel())).setRowCount(0);
      
      for (int i = 0; i < members.size(); i++) {
        o[0] = members.get(i).getUserID();
        o[1] = members.get(i).getName();
        o[2] = members.get(i).getJoin_year();
        o[3] = members.get(i).getCountry();
        o[4] = members.get(i).getPdaID();
        
        ((DefaultTableModel)(tbMembers.getModel())).addRow(o);
      }
    }
    catch(Exception e) {}
  }
  
  private final void updateRegions() {
    try {
      this.regions = new CRegion(parent.getConnection()).retrieveAll();
      
      ((DefaultListModel)(ltRegion.getModel())).clear();
      
      for (int i = 0; i < regions.size(); i++){
    	((DefaultListModel)(ltRegion.getModel())).addElement(regions.get(i).getDescription());
      }
      
      if (regions.size() == 0)
        JOptionPane.showMessageDialog(null, "There is no data on REGIONS table!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
    catch(Exception e) {}
  }
  
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        edID = new javax.swing.JTextField();
        edName = new javax.swing.JTextField();
        edYear = new javax.swing.JTextField();
        edAddress = new javax.swing.JTextField();
        edCountry = new javax.swing.JTextField();
        edPDA = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        ltRegion = new javax.swing.JList();
        btnAdd = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        btnModify = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbMembers = new javax.swing.JTable();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Editing MEMBER Table");

        jLabel2.setText("ID:");

        jLabel3.setText("Name:");

        jLabel4.setText("Address:");

        jLabel5.setText("Joined In:");

        jLabel6.setText("Country:");

        jLabel7.setText("PDA ID:");

        jLabel8.setText("Region ID:");

        ltRegion.setModel(new DefaultListModel());
        jScrollPane1.setViewportView(ltRegion);

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnRemove.setText("Remove");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });

        btnModify.setText("Modify");
        btnModify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifyActionPerformed(evt);
            }
        });

        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        tbMembers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "ID", "Name", "Joined Year", "Country", "PDA ID"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tbMembers);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(23, 23, 23)
                        .addComponent(edCountry, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(28, 28, 28)
                        .addComponent(edPDA, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(btnAdd)
                                .addGap(69, 69, 69)
                                .addComponent(btnRemove)
                                .addGap(69, 69, 69)
                                .addComponent(btnModify)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnRefresh))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel4))
                                        .addGap(23, 23, 23))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addGap(18, 18, 18)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(edID, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(edName, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(edAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(edYear, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(18, 18, 18)
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(14, 14, 14)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(edID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(edName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(edAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel2)
                                .addGap(24, 24, 24)
                                .addComponent(jLabel3)
                                .addGap(24, 24, 24)
                                .addComponent(jLabel4)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(edYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel6))
                            .addComponent(edCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel7))
                            .addComponent(edPDA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(110, 110, 110)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAdd)
                    .addComponent(btnRemove)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnModify)
                        .addComponent(btnRefresh)))
                .addGap(20, 20, 20))
        );
    }// </editor-fold>//GEN-END:initComponents

  private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
    if (!edID.getText().isEmpty() && !edAddress.getText().isEmpty() && !edName.getText().isEmpty() && 
        !edYear.getText().isEmpty() && !edCountry.getText().isEmpty() && !edPDA.getText().isEmpty() && ltRegion.getSelectedIndex() != -1) 
    {  
      try {
        Member m = new Member(edName.getText(), java.sql.Date.valueOf(edYear.getText() + "-01-01"), edCountry.getText(),
                              edPDA.getText(), Integer.parseInt(edID.getText()), regions.get(ltRegion.getSelectedIndex()).getRegionID());
        
        memberControl.create(m);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data added!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }//GEN-LAST:event_btnAddActionPerformed

  private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
    if (!edID.getText().isEmpty()) {
      try {
        Member m = new Member(edName.getText(), java.sql.Date.valueOf(edYear.getText() + "-01-01"), edCountry.getText(),
                              edPDA.getText(), Integer.parseInt(edID.getText()), 0);
    
        memberControl.delete(m);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data deleted!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }//GEN-LAST:event_btnRemoveActionPerformed

  private void btnModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifyActionPerformed
        if (!edID.getText().isEmpty() && !edAddress.getText().isEmpty() && !edName.getText().isEmpty() && 
            !edYear.getText().isEmpty() && !edCountry.getText().isEmpty() && !edPDA.getText().isEmpty() && ltRegion.getSelectedIndex() != -1) 
    {  
      try {
        Member m = new Member(edName.getText(), java.sql.Date.valueOf(edYear.getText() + "-01-01"), edCountry.getText(),
                              edPDA.getText(), Integer.parseInt(edID.getText()), regions.get(ltRegion.getSelectedIndex()).getRegionID());
    
        memberControl.update(m);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data updated!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }//GEN-LAST:event_btnModifyActionPerformed

  private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
    updateRegions();
    updateTable();
  }//GEN-LAST:event_btnRefreshActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnModify;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnRemove;
    private javax.swing.JTextField edAddress;
    private javax.swing.JTextField edCountry;
    private javax.swing.JTextField edID;
    private javax.swing.JTextField edName;
    private javax.swing.JTextField edPDA;
    private javax.swing.JTextField edYear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList ltRegion;
    private javax.swing.JTable tbMembers;
    // End of variables declaration//GEN-END:variables
}
